#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
账号通用字段定义和工具函数
"""

REQUIRED_FIELDS = ['user_id', 'nickname', 'is_logged', 'port']
OPTIONAL_FIELDS = ['avatar', 'keywords', 'replies', 'red_id']

def validate_account(account_data):
    """验证账号数据是否包含所有必要字段"""
    for field in REQUIRED_FIELDS:
        if field not in account_data:
            return False
    return True

def ensure_account_fields(account_data):
    """确保账号数据包含所有必要和可选字段，缺失的补空"""
    for field in REQUIRED_FIELDS:
        if field not in account_data:
            account_data[field] = '' if field != 'is_logged' else False
    for field in OPTIONAL_FIELDS:
        if field not in account_data:
            account_data[field] = ''
    return account_data

def replies_to_list(replies):
    """将回复话术转换为列表格式"""
    if isinstance(replies, list):
        return replies
    if isinstance(replies, str):
        return [line.strip() for line in replies.splitlines() if line.strip()]
    return []

def keywords_to_str(keywords):
    """将关键词转换为字符串格式"""
    if isinstance(keywords, str):
        return keywords
    if isinstance(keywords, list):
        return ','.join([str(k) for k in keywords if k])
    return ''